<?php

declare(strict_types=1);

namespace App\Service;

use App\Library\Common;

/**
 * 商家入驻详细信息服务
 * Class FlyShopApplysInfoService
 * @package App\Service
 */
class FlyShopApplysInfoService extends BaseService
{
    /**
     * 保存申请入驻信息详情
     * @param array $params
     * @return bool
     */
    public function saveApplyInfo(array $params): bool
    {
        // 申请入驻、申请修改信息、入驻单重新申请、升级云店 为新实例
        if (in_array($params['apply_order_type'], [1, 3, 4, 5])) {
            $instance = $this->getModelInstance();
        } else {
            $instance = $this->getModelInstance()->where('apply_id', '=', $params['apply_id'])->first();
        }
        // 获取要保存的数据
        if ($params['apply_order_type'] == 5) {
            unset($params['apply_order_type'], $params['user_id'], $params['id']);
            $data = $params;
        } else {
            $data = $this->getApplyData($params);
        }
        foreach ($data as $fields => $datum) {
            $instance->$fields = $datum;
        }
        if (!$instance->save()) {
            return false;
        }
        return true;
    }

    /**
     * 提供不同入驻类型保存的参数:shop_applys_info
     * @param array $params [apply_order_type] 1入驻信息 2完善信息 3修改信息
     * @return array
     */
    public function getApplyData(array $params): array
    {
        if ($params['apply_order_type'] == 1) {
            // 入驻信息
            $data = [
                'apply_id' => $params['apply_id'] ?? 0,
                'apply_user_id' => $params['user_id'],
                'shop_name' => $params['shop_name'],
                'link_name' => $params['link_name'],
                'phone' => $params['phone'],
                'shop_type' => $params['shop_type'],
                'address_locate' => $params['address_locate'],
                'address_info' => $params['address_info'] ?? "",
                'lat' => $params['lat'],
                'lng' => $params['lng']
            ];
        } elseif ($params['apply_order_type'] == 2) {
            $data = [
                // 完善信息
                'dc_id' => $params['dc_id'],
                'dc_list' => $params['dc_list'],
                'license_url' => empty($params['fileinfo']['license']) ? "" : Common::cdnurl($params['fileinfo']['license']['url']),
                'license_id' => empty($params['license_id']) ? 0 : (int)$params['license_id'],
                'cert_number' => $params['cert_number'] ?? "",
                'fullname' => $params['fullname'] ?? "",
                'id_number' => $params['id_number'] ?? "",
                'id_name' => $params['id_name'] ?? "",
                'cert_type' => $params['cert_type'],
                'figure_ids' => json_encode($params['figure_ids']),
                'figure_urls' => $params['figure_str'],
                'id_front_id' => empty($params['id_front_id']) ? 0 : (int)$params['id_front_id'],
                'id_front_url' => empty($params['fileinfo']['id_front']) ? "" : Common::cdnurl($params['fileinfo']['id_front']['url']),
                'id_back_id' => empty($params['id_back_id']) ? 0 : (int)$params['id_back_id'],
                'id_back_url' => empty($params['fileinfo']['id_back']) ? "" : Common::cdnurl($params['fileinfo']['id_back']['url']),
                'gender' => empty($params['gender']) ? 0 : (int)$params['gender'],
                'id_address' => $params['id_address'] ?? "",
                'birthday' => $params['birthday'] ?? null,
                'issuing_agency' => $params['issuing_agency'] ?? "",
                'valid_start_date' => $params['valid_start_date'] ?? null,
                'valid_end_date' => $params['valid_end_date'] ?? null,
                'business_term' => $params['business_term'] ?? "",
                'capital_social' => $params['capital_social'] ?? "",
                'corp' => $params['corp'] ?? "",
                'register_address' => $params['register_address'] ?? ""
            ];
        } elseif ($params['apply_order_type'] == 3) {
            // 修改店铺信息申请(非认证信息)
            $data = [
//                'industry_ids' => ',' . implode(',', $params['industry_ids']) . ',',
//                'industry_list' => $params['industry_list'],
                'dc_ids' => ',' . implode(',', $params['dc_ids']) . ',',
                'dc_list' => $params['dc_list'],
                'status' => 1, // 待审核
                'apply_id' => $params['apply_id'] ?? 0,
                'apply_user_id' => $params['user_id'],
                'shop_name' => $params['shop_name'],
                'link_name' => $params['link_name'],
                'phone' => $params['phone'],
                'shop_type' => $params['shop_type'],
                'address_id' => $params['address_id'],
                'address_info' => $params['address_info'] ?? ""
            ];
        } else {
            // 被驳回重新入驻单
            $data = [
                'apply_id' => $params['apply_id'] ?? 0,
                'apply_user_id' => $params['user_id'],
                'shop_name' => $params['shop_name'],
                'link_name' => $params['link_name'],
                'phone' => $params['phone'],
                'shop_type' => $params['shop_type'],
                'address_locate' => $params['address_locate'],
                'address_info' => $params['address_info'] ?? "",
//                'industry_ids' => implode(',', $params['industry_ids']),
//                'industry_list' => $params['industry_list'],
                'dc_id' => $params['dc_id'],
                'dc_list' => $params['dc_list'],
                'license_url' => empty($params['fileinfo']['license']) ? "" : Common::cdnurl($params['fileinfo']['license']['url']),
                'lat' => $params['lat'],
                'lng' => $params['lng'],
                'license_id' => empty($params['license_id']) ? 0 : (int)$params['license_id'],
                'cert_number' => $params['cert_number'] ?? "",
                'fullname' => $params['fullname'] ?? "",
                'id_number' => $params['id_number'] ?? "",
                'id_name' => $params['id_name'] ?? "",
                'cert_type' => $params['cert_type'],
                'figure_ids' => json_encode($params['figure_ids']),
                'figure_urls' => $params['figure_str'],
                'id_front_id' => empty($params['id_front_id']) ? 0 : (int)$params['id_front_id'],
                'id_front_url' => empty($params['fileinfo']['id_front']) ? "" : Common::cdnurl($params['fileinfo']['id_front']['url']),
                'id_back_id' => empty($params['id_back_id']) ? 0 : (int)$params['id_back_id'],
                'id_back_url' => empty($params['fileinfo']['id_back']) ? "" : Common::cdnurl($params['fileinfo']['id_back']['url']),
                'gender' => empty($params['gender']) ? 0 : (int)$params['gender'],
                'id_address' => $params['id_address'] ?? "",
                'birthday' => $params['birthday'] ?? null,
                'issuing_agency' => $params['issuing_agency'] ?? "",
                'valid_start_date' => $params['valid_start_date'] ?? null,
                'valid_end_date' => $params['valid_end_date'] ?? null,
                'business_term' => $params['business_term'] ?? "",// 营业期限
                'capital_social' => $params['capital_social'] ?? "",
                'corp' => $params['corp'] ?? "",
                'register_address' => $params['register_address'] ?? ""
            ];
        }
        return $data;
    }

    /**
     * 获取申请单详情
     * @param int $apply_id
     * @return array
     */
    public function getApplyInfo(int $apply_id): array
    {
        $res = $this->getModelInstance()->where(['apply_id' => $apply_id])->first();
        if (empty($res)) {
            return [];
        }
        return $res->toArray();
    }

}